import axios, { AxiosResponse, } from "axios";
import jsonBigint from "json-bigint";

const BASE_URL = "http://localhost:8848";
const TIME_OUT = 20000;
//创建请求实例，设置基础配置
export const httpRequest = axios.create({
  timeout: TIME_OUT,
  baseURL: BASE_URL,
  headers: {
    "Content-Type": "application/json",
    // 'Content-Type': 'application/x-www-form-urlencoded'
    // 'Content-Type': 'multipart/form-data;'
    // "Redirect-Header": false,
  },
});

export const $setToken = (token)=>{
  httpRequest.defaults.headers.common['token']= token;
}

httpRequest.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

httpRequest.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);


export const get = async (
  url: string,
  data: RequestData,
) => {
  let res = await httpRequest.get(url, {
    params: data,
  });
  let response: RequestData = await checkRes(res);
  return res.data;
};

export const post = async (url: string, data: RequestData) => {
  let res = await httpRequest({
    method: "post",
    url,
    data,
  });
  res = await checkRes(res);
  return res.data;
};

const checkRes = async (response: AxiosResponse<any, any>) => {
  return response;
};

export interface RequestData {
  [k: string]: any;
}

export interface Headers {
  [k: string]: string;
}